package com.owg.jigsaw.blocktypes;

import java.awt.Component;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import com.owg.jigsaw.WBlock;
import com.owg.jigsaw.WBlockBase;
import com.owg.jigsaw.WRoot;
import com.owg.jigsaw.slots.GenericSingleSlot;

import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
/**
 * A window block. A window block has a text field and a single panel slot.
 */

public class WindowBlock extends WBlockBase implements WComponentBlock
{
	private GenericSingleSlot<WComponentBlock> contentPane;
	private GenericSingleSlot<WLabelBlock> title;

	//public final PFieldOptional<WBlock> contentPane = new PFieldOptional<WBlock>(this, WBlock.class);
	
	public WindowBlock(WRoot root, boolean isPaletteBlock)
	{
		super(root, Frame.class, isPaletteBlock, Color.PINK);
		getCell(0,0).installContentPane(new Group(new Text("Window        Title:")));
		getCell(0,0).southSlots.add(contentPane = new GenericSingleSlot<>(WComponentBlock.class, root.getTypeHierarchy().getTypeNode(Component.class)));
		getCell(0,0).eastSlots.add(title = new GenericSingleSlot<>(WLabelBlock.class, root.getTypeHierarchy().getTypeNode(Label.class)));
	}

	@Override
	public WBlock copy(boolean withChildren)
	{
		WindowBlock b = new WindowBlock(root, false);
		/* TODO if(withChildren)
		{
			b.contentPane.set(contentPane.get());
		}*/
		return b;
	}

	public void mkFrame()
	{
		Frame f = (Frame)getComponent();
		f.setVisible(true);
	}

	@Override
	public Component getComponent()
	{
		Frame f = new Frame();
		if(!title.child.isEmpty())
			f.setTitle(title.get().title.get());
		
		if(!contentPane.child.isEmpty())
			f.add(contentPane.get().getComponent());
		f.pack();
		f.addWindowListener(new WindowAdapter()
		{
			@Override
			public void windowClosing(WindowEvent e)
			{
				f.dispose();
			}
		});
		f.setLocationRelativeTo(null);
		return f;
	}
	
	
}
